package me.baicai.modules.belldandy.gcb.rest;

import me.baicai.aop.log.Log;
import me.baicai.modules.belldandy.gcb.domain.Project;
import me.baicai.modules.belldandy.gcb.service.ProjectService;
import me.baicai.modules.belldandy.gcb.service.dto.ProjectQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @author 超级大白菜
* @date 2020-03-31
*/
@Api(tags = "Project管理")
@RestController
@RequestMapping("/api/project")
public class ProjectController {

    private final ProjectService projectService;

    public ProjectController(ProjectService projectService
                             ) {
        this.projectService = projectService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('project:list')")
    public void download(HttpServletResponse response, ProjectQueryCriteria criteria) throws IOException {
        projectService.download(projectService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询Project")
    @ApiOperation("查询Project")
    @PreAuthorize("@el.check('project:list')")
    public ResponseEntity getProjects(ProjectQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增Project")
    @ApiOperation("新增Project")
    @PreAuthorize("@el.check('project:add')")
    public ResponseEntity create(@Validated @RequestBody Project resources){
        return new ResponseEntity<>(projectService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改Project")
    @ApiOperation("修改Project")
    @PreAuthorize("@el.check('project:edit')")
    public ResponseEntity update(@Validated @RequestBody Project resources){
        projectService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping(value = "/{id}")
    @Log("删除Project")
    @ApiOperation("删除Project")
    @PreAuthorize("@el.check('project:del')")
    public ResponseEntity delete(@PathVariable Integer id){
        projectService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @PutMapping(value = "/set")
    @Log("修改项目的关联受益人")
    @ApiOperation("修改项目的关联受益人")
    public ResponseEntity updateUser(@RequestBody Project resources){
        projectService.updateUser(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping(value = "/setDividend")
    @Log("修改项目的关联受益人")
    @ApiOperation("修改项目的关联受益人")
    public ResponseEntity setDividend(@RequestBody Project resources){
        projectService.updateUserDividend(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @GetMapping(value = "/all")
    @Log("查询Project")
    @ApiOperation("查询Project")
    public ResponseEntity getProjects(){
        return new ResponseEntity<>(projectService.queryAll(),HttpStatus.OK);
    }
}
